/**
* @depends jquery.js
*/


(function($){

	var Cache = {};

	$.wajax = function() {
		var args = [];
		var cache = false;
		var callback = new Function();
		for( var i=0; i<arguments.length; i++ ) {
			var arg = arguments[i];
			if( $.isFunction( arg ) ) {
				callback = arg;
				continue;
			}
			if( typeof arg == 'boolean' ) {
				cache = true;
				continue;
			}
			args.push( arg );
		}
		
		args.push( function( json ) {
			$.wajax.callback( json, callback );
			if( cache ) {
				Cache[ args[0] ] = json;
			}
		} );
		args.push( 'json' );
		
		if( cache && Cache[ args[0] ] && (Cache[ args[0] ].status == 'success') ) {
			args[args.length-2].call( this, Cache[ args[0] ] );
		}
		else {
			$.post.apply( this, args );
		}
	
	}
	
	$.wajax.callback = function( json, callback ) {
		switch( json.status ) {
			case 'exception' :
				var dialog = $.window.create()
					.dialog( {
						title: 'Error',
						message: json.message,
						icon: $.dialog.icons.error,
						buttons: {
							ok: function() {
								dialog.dialog( 'close' );
							}
						}
					} )
					.dialog( 'open' );
					setTimeout( function() {
						dialog.dialog( 'close' );
					}, 3000 );
				break;
		}
		callback.call( this, json );
	}

})(jQuery);
